package sg.edu.nus.iss.club.gui;
import java.awt.event.ActionEvent;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import sg.edu.nus.iss.club.Booking;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class AddBookingDialog extends OkCancelDialog {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JLabel jLabel1;
	private JLabel jLabel2;
	private JLabel jLabel4;
	private JLabel jLabel5;
	private JLabel dateLabel;
	private JLabel memberLabel;
	private JLabel jLabel6;
	private JLabel facilityLabel;
	private JTextField endTimeTextField;
	private JLabel jLabel3;
	private JTextField startTimeTextField;
	public ClubApplication clubApplication;

	public AddBookingDialog(ClubApplication clubApplication, JFrame frame) {
		super(frame);
		this.clubApplication = clubApplication;
		initGUI();
	}
		
	private void initGUI() {
		try {
			{
				getContentPane().setLayout(null);
				{
					jLabel1 = new JLabel();
					getContentPane().add(jLabel1);
					jLabel1.setText("Facility");
					jLabel1.setBounds(12, 12, 83, 16);
				}
				{
					jLabel2 = new JLabel();
					getContentPane().add(jLabel2);
					jLabel2.setBounds(12, 38, 83, 16);
					jLabel2.setText("Member");
				}
				{
					okButton = new JButton();
					getContentPane().add(okButton);
					okButton.setText("OK");
					okButton.setBounds(107, 158, 46, 23);
					okButton.addActionListener(new ButtonActionListener() {
						public void actionMethod(ActionEvent e) {
							Calendar cal_start = Calendar.getInstance();
							Calendar cal_end = Calendar.getInstance();
							String completeDateFormat = Booking.DATE_FORMAT + " H:mm";
							DateFormat df1 = new SimpleDateFormat(completeDateFormat);
							try {
								String completeStartDate = clubApplication.getMainWindow().getBookingPanel().getDate() + " " + startTimeTextField.getText();
								String completeEndDate = clubApplication.getMainWindow().getBookingPanel().getDate() + " " + endTimeTextField.getText();
								cal_start.setTime(df1.parse(completeStartDate));
								cal_end.setTime(df1.parse(completeEndDate));
							} catch (ParseException ex) {
								ex.printStackTrace();
								JOptionPane.showMessageDialog(null, "Invalid Date Format", "Error", JOptionPane.ERROR_MESSAGE);
								return;
							}
							Date startDate = (Date)cal_start.getTime();
							Date endDate = (Date)cal_end.getTime();
							clubApplication.addBooking(startDate, endDate);
							clubApplication.getMainWindow().refresh();
						}
					});
				}
				{
					cancelButton = new JButton();
					getContentPane().add(cancelButton);
					cancelButton.setText("Cancel");
					cancelButton.setBounds(165, 158, 69, 23);
					cancelButton.addActionListener(new CancelButtonActionListener());
				}
				{
					startTimeTextField = new JTextField();
					getContentPane().add(startTimeTextField);
					startTimeTextField.setBounds(147, 88, 193, 23);
				}
				{
					jLabel3 = new JLabel();
					getContentPane().add(jLabel3);
					jLabel3.setText("Date");
					jLabel3.setBounds(12, 63, 83, 16);
				}
				{
					jLabel4 = new JLabel();
					getContentPane().add(jLabel4);
					jLabel4.setText("Start time (hh:mm)");
					jLabel4.setBounds(12, 91, 125, 16);
				}
				{
					endTimeTextField = new JTextField();
					getContentPane().add(endTimeTextField);
					endTimeTextField.setBounds(146, 116, 193, 23);
				}
				{
					facilityLabel = new JLabel();
					getContentPane().add(facilityLabel);
					if (clubApplication.getMainWindow().getFacilityPanel().getSelectFacility() != null)
						facilityLabel.setText(clubApplication.getMainWindow().getFacilityPanel().getSelectFacility().toString());
					facilityLabel.setBounds(146, 12, 217, 16);
				}
				{
					jLabel6 = new JLabel();
					getContentPane().add(jLabel6);
					jLabel6.setText("Member");
					jLabel6.setBounds(12, 38, 83, 16);
				}
				{
					memberLabel = new JLabel();
					getContentPane().add(memberLabel);
					if (clubApplication.getMainWindow().getMemberPanel().getSelectedMember() != null)
						memberLabel.setText(clubApplication.getMainWindow().getMemberPanel().getSelectedMember().toString());
					memberLabel.setBounds(144, 38, 83, 16);
				}
				{
					dateLabel = new JLabel();
					getContentPane().add(dateLabel);
					dateLabel.setText(clubApplication.getMainWindow().getBookingPanel().getDate());
					dateLabel.setBounds(146, 63, 83, 16);
				}
				{
					jLabel5 = new JLabel();
					getContentPane().add(jLabel5);
					jLabel5.setText("End time (hh:mm)");
					jLabel5.setBounds(12, 120, 125, 16);
					//jLabel4.setBounds(12, 91, 125, 16);
				}
			}
			this.setSize(372, 230);
			setTitle("Book Facility");
			setLocation(200, 200);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
